/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hessingerbg.codebehind;

import hessingerbg.gui.Version;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

/**
 * Log Class allows for logging of the ICEPIC project. Used for Debugging Purposes
 * @author Michael King
 */
public class log {
    private static String filename = "ICEPIC.rtf";
    public static JTextPane out;
    private static String timeformat = "hh:mm:ss";
    private static int on = 1;
    private static DateFormat SDF = new SimpleDateFormat(timeformat);
  //  private static Date date;

    /**
     * Writes the Header in the Log
     */
    public static void start()
    {
        StyledDocument doc = out.getStyledDocument();
        if(on==1)
        {      
            try {
                doc.insertString(0, "\n--------------------------------------------------\n", null);
                doc.insertString(0, "   HessingerBG Session " + (new SimpleDateFormat("dd/MM/yyyy 'at' hh:mm:ss")).format(new Date()) + "\n", null);
                doc.insertString(0, "   HessingerBG " + Version.version + "\n", null);
                doc.insertString(0, "--------------------------------------------------\n", null);
            } catch (BadLocationException ex) {
                Logger.getLogger(log.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    

    /**
     * Prints our a message.
     * @param x
     */
    public static void println(String x)
    {
        StyledDocument doc = out.getStyledDocument();
        if(on == 1)
        {
            try {
                doc.insertString(0, SDF.format(new Date()) + ": " + sun.reflect.Reflection.getCallerClass(2).getName() + ": " + x + "\n", null);
            } catch (BadLocationException ex) {
                Logger.getLogger(log.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Prints out an exception message
     * @param e
     */
    public static void println(Exception e)
    {
        StyledDocument doc = out.getStyledDocument();
        if(on == 1)
        {
            try {
                doc.insertString(0, SDF.format(new Date()) + ": " + e.getMessage() + "\n", null);
                doc.insertString(0, SDF.format(new Date()) + ": " + e.toString() + "\n", null);
                doc.insertString(0, SDF.format(new Date()) + ": " + e.getLocalizedMessage() + "\n", null);
            } catch (BadLocationException ex) {
                Logger.getLogger(log.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Leaves end Timestamp to the session, if this does not show up in the log, the program crashed.
     */
    public static void end()
    {
        if(on == 1)
        {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(filename, true));
            out.write("Session End " + (new SimpleDateFormat("dd/MM/yyyy 'at' hh:mm:ss")).format(new Date()));
            out.close();
        } catch (IOException ex) {
            
        }
        }

    }
}
